<?php
require_once('lib/conf.config.php');
include_once('lib/func.gdbundle.php');
include_once('lib/class.colorlib.php');
include_once('lib/class.psdreader.php');
foreach (glob(dirname(__FILE__).'/'.IMARTY_PLUGIN_DIR.'*.php') as $filename){
   include_once($filename);
}

/*
Imarty V0.10
Author: Chao Xu
Website: www.webdevlogs.com
Change:
0.10
Reconstruct the dirctories
0.08
imagefilter() and imageconvolution() workaround
0.07
added ID
0.04
Can load source image as a layer
support GIF, PNG, JPEG, PSD, WBMP, XBM
0.03
Finished the handler system
Plugins are much better
0.02
Seprated Imarty with AnImarty
To do: 
Create are validation module present
Extend the standard drawing library
Easier data manipulation, less type casting
Develop a better plug-in system
*/
class imarty{
	var $layer;
	var $h;
	var $w;
	var $id;
	var $cache;
	var $image;
	var $handle;
	
function __autoload($class){
   include(IMARTY_PLUGIN_DIR.$class.'.php');
}

function simple_xml_input($xml){
	$this->layer = $xml->layer;
	$this->h = (string) $xml['h'];
	$this->w = (string) $xml['w'];
	$this->cache = (string) $xml['cache'];
	$this->id = (string) $xml['id'];
	if(!$this->id){
		$this->id = serialize($this);
	}
	//ID will be important for cache
	$this->id = md5($this->id);
}

function imarty($xml='', $string=false){
	include('lib/conf.handles.php');
	$this->handle = $handle;
	if($xml!=''){
		$this->load_xml($xml, $string);
	}
}

function load_xml($xml, $string=false){
	if(!$string){
		$xml = simplexml_load_file($xml);
	}else{
		$xml = simplexml_load_string($xml);
	}
	$this->layer = $xml->layer;
	$this->h = (string) $xml['h'];
	$this->w = (string) $xml['w'];
	$this->cache = (string) $xml['cache'];
	$this->id = (string) $xml['id'];
	if(!$this->id){
		$this->id = serialize($this);
	}
	//ID will be important for cache
	$this->id = md5($this->id);
}

function render(){
	$image = imagecreatetruecolor($this->w, $this->h);
	$n = 0; $count = count($this->layer);
	while($n < $count){
		$layer = $this->render_layer($n);
		if($this->layer[$n]['opacity']==''){
			$this->layer[$n]['opacity'] = 100;
		}

		if($this->layer[$n]['copy']){
		imagecopy($image, $layer, $this->layer[$n]['x'], $this->layer[$n]['y'], 0, 0, $this->layer[$n]['w'], $this->layer[$n]['h']);
		}else{
		imagecopymerge($image, $layer, $this->layer[$n]['x'], $this->layer[$n]['y'], 0, 0, $this->layer[$n]['w'], $this->layer[$n]['h'], $this->layer[$n]['opacity']);
		}

		imagedestroy($layer);
		++$n;
	}
	
	$this->image = $image;
}

function imagecreatefrom($source){
	$type = getimagesize($source);
		$type = $type[2];
		switch($type) {
			case 1:
			   return imagecreatefromgif($source);
			break;
			case 2:
			   return imagecreatefromjpeg($source);
			break;
			case 3:
			   return imagecreatefrompng($source);
			break;
			case 5:
			   return imagecreatefrompsd($source);
			break;
			case 15:
			   return imagecreatefromwbmp($source);
			break;
			case 16:
			   return imagecreatefromxbm($source);
			break;
			default:
				return imagecreatetruecolor(1,1);
			break;
	}
}


function render_layer($n){
	$layer = $this->layer[$n];
	if($layer['src']){
		$image = $this->imagecreatefrom($layer['src']);
	}else{
		$image = imagecreatetruecolor($layer['w'],$layer['h']);
		if($layer['color']){
			$color = imarty_color($image, $layer['color']);
			if(($color >> 24)==127){
		$image = imagecreatetransparent($layer['w'],$layer['h']);
		    }else{
		    	imagefill($image, 0, 0, $color);
		    }
    	}
			
		}
	if(function_exists('imageantialias')){
		if($layer['antialias'] == 'on'){
			imageantialias($image, true);
		}else{
			imageantialias($image, false);
		}
	}
	//imageantialias($image, (bool)$layer['antialias']);
	$children = $layer->children();
	$count = count($children);
	$z = 0;
	while($z < $count){
		$element = dom_import_simplexml($children[$z]);
		$name = $element->nodeName;
		$hand = $this->handle[$name];
		if($hand['type']==='static'){
			call_user_func(array($hand['class'], $hand['method']), $image, $children[$z]);
		}elseif($hand['type']==='object'){
			$class = new $hand['class'];
			$class->$hand['method']($image, $children[$z]);
		}else{
			$hand['method']($image, $children[$z]);
		}
		++$z;
	}
	return $image;
}

function output_image($format = 'png', $filename = null, $quality=9, $filters=PNG_NO_FILTER){
	switch ($format) {
		case 'gif':
			if($filename===null){
				header("Content-type: image/gif");
				imagegif($this->image);
			}else{
				imagegif($this->image,$filename);
			}
		break;
		case 'jpg':
			if($filename===null){
				header("Content-type: image/jpg");
				imagejpeg($this->image,null,$quality);
			}else{
				imagejpeg($this->image,$filename,$quality);
			}
		break;
		case 'wbmp':
			if($filename===null){
				header("Content-type: image/vnd.wap.wbmp");
				imagewbmp($this->image);
			}else{
				imagewbmp($this->image,$filename);
			}
		break;
		default:
			if($filename===null){
				header("Content-type: image/png");
				imagepng($this->image, null, $quality, $filters);
			}else{
				imagepng($this->image, $filename, $quality, $filters);
			}
		break;
	}
}

function return_image($format = 'gif'){
	ob_start();
	switch ($format) {
		case 'png':
			imagepng($this->image);
		break;
		case 'jpg':
			imagejpeg($this->image);
		break;
		case 'wbmp':
			imagewbmp($this->image);
		break;
		default:
			imagegif($this->image);
		break;
	}
	$image = ob_get_contents();
	ob_end_clean();
	return $image;
}
}

?>